<template>
  <div class="course-container">
    <!-- 头部搜索区域 -->
    <div class="hd">
      <el-form :model="courseForm" inline>
        <el-form-item>
          <el-input
            v-model="courseForm.name"
            placeholder="输入课程名称"
            size="mini"
            style="width: 200px; margin-right: 20px"
          />
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-search
"
            @click="search"
            >搜索</el-button
          >
          <el-button
            type="warning"
            size="mini"
            icon="el-icon-refresh-left

"
            @click="reset"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-form inline>
        <el-form-item>
          <el-button
            type="success"
            size="mini"
            icon="el-icon-plus"
            @click="addCourse"
            >添加</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格区域 -->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%; height: 650px; overflow-y: auto"
    >
      <el-table-column label="课程名称" prop="title" />
      <el-table-column prop="intro" label="课程简介" />
      <el-table-column prop="price" label="课程价格" />
      <el-table-column prop="courseTime" label="课程时长(分钟)" />
      <el-table-column prop="status" label="课程类型">
        <template slot-scope="scope">
          <el-tag type="success" size="mini" v-if="scope.row.status === 0"
            >免费</el-tag
          >
          <el-tag type="warning" size="mini" v-else>付费</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="author" label="授课老师" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="edit(scope.row.id)"
            >编辑</el-button
          >
          <el-button type="text" size="mini" @click="del(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      :total="total"
      :page.sync="currentPage"
      :limit.sync="page"
      @pagination="getData"
    />

    <!-- 新增编辑对话框 -->
    <el-dialog
      :title="courseId ? '编辑课程' : '新增课程'"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="addformRef" :model="addForm" label-width="80px">
        <el-form-item label="课程名称" prop="title">
          <el-input v-model="addForm.title" />
        </el-form-item>
        <el-form-item label="课程价格" prop="price">
          <el-input v-model="addForm.price" />
        </el-form-item>
        <el-form-item label="课程类型" prop="status">
          <el-radio v-model="addForm.status" :label="1">付费</el-radio>
          <el-radio v-model="addForm.status" :label="0">免费</el-radio>
        </el-form-item>
        <el-form-item label="简介" prop="intro">
          <el-input v-model="addForm.intro" type="textarea" />
        </el-form-item>
        <el-form-item label="课程时长" prop="courseTime">
          <el-input v-model="addForm.courseTime" />
        </el-form-item>
        <el-form-item label="授课老师" prop="author">
          <el-input v-model="addForm.author" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCourse,
  queryList,
  delCourse,
  course,
  updateCourse,
} from "@/api/course";
import { Message } from "element-ui";
export default {
  data() {
    return {
      courseForm: {},
      tableData: [],
      dialogVisible: false,
      addForm: {},
      total: 0,
      // 当前页
      currentPage: 1,
      // 每页多少条
      page: 20,
      courseId: "",
    };
  },
  created() {
    this.getData();
  },
  methods: {
    addCourse() {
      this.dialogVisible = true;
    },
    submitForm() {
      if (this.courseId) {
        updateCourse({
          title: this.addForm.title,
          price: this.addForm.price,
          intro: this.addForm.intro,
          author: this.addForm.author,
          status: this.addForm.status,
          courseTime: this.addForm.courseTime,
          id: this.courseId,
        })
          .then((res) => {
            if (res.code === 200) {
              Message.success("更新课程成功");
              this.dialogVisible = false;
              this.getData();
              this.courseId = "";
            }
          })
          .catch((err) => {
            this.dialogVisible = false;
            this.getData();
            this.courseId = "";
          });
      } else {
        addCourse({
          title: this.addForm.title,
          price: this.addForm.price,
          intro: this.addForm.intro,
          author: this.addForm.author,
          status: this.addForm.status,
          courseTime: this.addForm.courseTime,
        })
          .then((res) => {
            if (res.code === 200) {
              Message.success("添加课程成功");
              this.dialogVisible = false;
              this.getData();
              this.courseId = "";
            }
          })
          .catch((err) => {
            this.dialogVisible = false;
            this.getData();
            this.courseId = "";
          });
      }
    },
    getData() {
      queryList({
        size: this.page,
        current: this.currentPage,
      }).then((res) => {
        this.total = res.data.total;
        this.tableData = res.data.records;
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.addForm = {};
      this.getData();
      this.courseId = "";
    },
    cancel() {
      this.dialogVisible = false;
      this.addForm = {};
      this.getData();
      this.courseId = "";
    },
    search() {
      queryList({
        size: this.page,
        current: this.currentPage,
        title: this.courseForm.name,
      }).then((res) => {
        this.total = res.data.total;
        this.tableData = res.data.records;
      });
    },
    reset() {
      this.courseForm = {};
      this.getData();
    },
    del(id) {
      this.$confirm("此操作将永久删除该课程, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delCourse(id).then((res) => {
            if (res.code === 200) {
              Message.success("删除成功");
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({ type: "info", message: "已取消删除" });
          this.getData();
        });
    },
    edit(id) {
      this.courseId = id;
      this.dialogVisible = true;
      course(id).then((res) => {
        this.addForm = res.data;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.course-container {
  height: 100%;
  padding: 20px 40px;
  .hd {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
